package com.source.boot01.uploadcontroller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.source.boot01.esayexcel.ReqCustomerDailyImport;
import com.source.boot01.esayexcel.RespCustomerDailyImport;
import com.source.boot01.utils.ExtelRead;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.util.List;
import com.google.common.collect.Lists;
/**
 * @Author wengqy
 * @Date 2021/2/2
 */

@Controller
public class EasyExcelController {







    @PostMapping("/importEasyExcel")
    @ResponseBody
    public List<ReqCustomerDailyImport> importCustomerDaily(@RequestParam MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        List<ReqCustomerDailyImport> reqCustomerDailyImports = EasyExcel.read(inputStream)
                .head(ReqCustomerDailyImport.class)
                // 设置sheet,默认读取第一个
                .sheet()
                // 设置标题所在行数
                .headRowNumber(2)
                .doReadSync();
        System.out.println(reqCustomerDailyImports);
        return reqCustomerDailyImports;
    }


    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        // 生成数据
        List<RespCustomerDailyImport> respCustomerDailyImports = Lists.newArrayList();
        for (int i = 0; i < 50; i++) {
            RespCustomerDailyImport respCustomerDailyImport = RespCustomerDailyImport.builder()
                    .misCode(String.valueOf(i))
                    .customerName("customerName" + i)
                    .monthlyQuota(new BigDecimal(String.valueOf(i)))
                    .accountReceivableQuota(new BigDecimal(String.valueOf(i)))
                    .dailyInterestRate(new BigDecimal(String.valueOf(i))).build();
            respCustomerDailyImports.add(respCustomerDailyImport);
        }

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("导出", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), RespCustomerDailyImport.class)
                .sheet("sheet0")
                // 设置字段宽度为自动调整，不太精确
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(respCustomerDailyImports);
    }




    @RequestMapping("/easyupload")
    public void essyExcelUpload(MultipartFile file, HttpServletRequest request){


    }





    @RequestMapping("/ExcelWriter")
    @ResponseBody    // 恒不跳转，以json格式显示在浏览器上
    public List<List<String>> ExcelWriter(){
        List<List<String>> lists = ExtelRead.testRead();
        if(lists != null){
            System.out.println(lists.size());
        }else{
            System.out.println("异常");
        }
        return lists;
    }






}
